require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'select2': '/js/lib/select2.min',
        'bootstrap': '/js/lib/bootstrap.min'
    },
    shim: {
        'bootstrap': ['jquery']
    }
});

var myFun = {};

require(['jquery', 'doT', 'select2', 'common', 'bootstrap'], function($, doT, select2, common) {
    $(function() {
        var role = sessionStorage.getItem('role');

        function initSchool() {
            $.get(common.urlBase + '/school/allSchool.json', function(data, statu) {
                data = eval('(' + data + ')');
                if (data['ok']) {
                    var schoolTmpl = doT.template($('#template_school').text());
                    $('#selectSchool').html(schoolTmpl(data.data));
                    $('#selectSchool').select2();
                    if (role != 1) {
                        $('#selectSchool').attr('disabled', true);
                    }
                } else {
                    alert('获取学校信息失败！');
                }
            });
        }
        initSchool();



        $('#add_canteen').submit(function() {
            sendImg();
            return false;
        });

        function sendImg() {
            var formData = new FormData();
            if (!$('#fileu').val()) {
                $('#noticeMsg').text('请选择图片！');
                $('#noticeModal').modal('show');
                return false;
            } else {
                formData.append('fileu', $('#fileu')[0].files[0]);
                $.ajax({
                    url: common.urlBase + '/file/fileUpload.json',
                    data: formData,
                    type: 'POST',
                    processData: false,
                    contentType: false,
                    success: function(response) {
                        var data = eval('(' + response + ')');
                        if (data['ok']) {
                            myFun.shop_logo = data.data.file;
                            sendData();
                        } else {
                            $('#noticeMsg').text('上传图片失败！');
                            $('#noticeModal').modal('show');
                            return false;
                        }
                    }
                });
            }
        }

        function sendData() {
            var name = $('#shop_name').val();
            var shop_url = $('#shop_url').val();
            var youzan_group_id = $('#youzan_group_id').val();
            var master_id = $('#master_id').val();
            var school_id = $('#selectSchool').val();


            var d = {
                'shop_name': name,
                'shop_logo': myFun.shop_logo,
                'shop_url': shop_url,
                'school_id': school_id,
                'youzan_group_id': youzan_group_id,
                'master_id': master_id
            };

            $.post(common.urlBase + '/shop/addshop/add.json', d, function(data, statu) {
                var data = eval('(' + data + ')');
                if (data['ok']) {
                    $('#noticeMsg').text('添加成功！');
                    $('#noticeModal').modal('show');
                    window.location = '/views/canteen_manage.html';
                } else {
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');
                }
            });
        }

        $('.sidebar ul li').each(function() {
            if ($(this).text() == '食堂管理') {
                $(this).parent().css('display', 'block');
                $(this).children().css('color', '#46A3FF');
            }
        });

        $('#fileu').on('change', function() {
            var reader = new FileReader();
            reader.onload = function(e) {
                $('#preview').attr('src', e.target.result);
                if (/image/.test($('#fileu')[0].files[0].type)) {
                    $('#preview').css('display', 'block');
                    $('#preview').show();
                } else {
                    $('#errorMessage').show();
                }
            };
            reader.readAsDataURL($('#fileu')[0].files[0]);

        });

    });


});
